package com.nervenets.web.controller.order;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class ShopCart implements Serializable {
    private boolean selected;
    private long typeId;
    private String typeName;
    private List<ShopCartItem> items = new ArrayList<>();

    public ShopCart(boolean selected, long typeId, String typeName) {
        this.selected = selected;
        this.typeId = typeId;
        this.typeName = typeName;
    }

    public int removeItem(ShopCartItem item) {
        items.remove(item);
        return items.size();
    }

    public int updateItem(ShopCartItem item) {
        if (items.contains(item)) {
            items.set(items.indexOf(item), item);
            /*if (0 == item.getTotal()) {
                items.remove(item);
            } else {
                items.set(items.indexOf(item), item);
            }*/
        } else {
            items.add(item);
        }
        return items.size();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof ShopCart)) return false;
        ShopCart shopCart = (ShopCart) o;
        return typeId == shopCart.typeId;
    }

    @Override
    public int hashCode() {

        return Objects.hash(typeId);
    }
}
